/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace SilverChart.Log {
    /// <summary>
    /// Description of LoggerConfigurations.
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public class JsonLoggerConfigurations : LoggerConfigurations {
        private String _debugFormat;
        private String _infoFormat;
        private String _warnFormat;
        private String _errorFormat;
        private String _fatalFormat;
        private String _globalLogLevel;

        public JsonLoggerConfigurations()
            : base() {
        }

        [JsonProperty("loggers")]
        public override List<String> Loggers { get; set; }

        [JsonProperty("debug_format")]
        public String _DebugFormat {
            get {
                return _debugFormat;
            }
            set {
                _debugFormat = value;
                DebugFormat = ReplaceFormatString(_debugFormat);
            }
        }
        public override String DebugFormat { get; set; }

        [JsonProperty("info_format")]
        public String _InfoFormat {
            get {
                return _infoFormat;
            }
            set {
                _infoFormat = value;
                InfoFormat = ReplaceFormatString(_infoFormat);
            }
        }
        public override String InfoFormat { get; set; }

        [JsonProperty("warn_format")]
        public String _WarnFormat {
            get {
                return _warnFormat;
            }
            set {
                _warnFormat = value;
                WarnFormat = ReplaceFormatString(_warnFormat);
            }
        }
        public override String WarnFormat { get; set; }

        [JsonProperty("error_format")]
        public String _ErrorFormat {
            get {
                return _errorFormat;
            }
            set {
                _errorFormat = value;
                ErrorFormat = ReplaceFormatString(_errorFormat);
            }
        }
        public override String ErrorFormat { get; set; }

        [JsonProperty("fatal_format")]
        public String _FatalFormat {
            get {
                return _fatalFormat;
            }
            set {
                _fatalFormat = value;
                FatalFormat = ReplaceFormatString(_fatalFormat);
            }
        }
        public override String FatalFormat { get; set; }

        [JsonProperty("create_div")]
        public override Boolean CreateLogDiv { get; set; }

        [JsonProperty("div_name")]
        public override String DivName { get; set; }


        [JsonProperty("level")]
        public String _GlobalLogLevel {
            get {
                return _globalLogLevel;
            }
            set {
                _globalLogLevel = value;
                switch (_globalLogLevel.ToLower()) {
                    case "debug":
                        GlobalLogLevel = LogLevel.Debug;
                        break;
                    case "info":
                        GlobalLogLevel = LogLevel.Info;
                        break;
                    case "warn":
                        GlobalLogLevel = LogLevel.Warn;
                        break;
                    case "error":
                        GlobalLogLevel = LogLevel.Error;
                        break;
                    case "fatal":
                        GlobalLogLevel = LogLevel.Fatal;
                        break;
                    default:
                        GlobalLogLevel = LogLevel.Unknown;
                        break;
                }
            }
        }
        public override LogLevel GlobalLogLevel { get; set; }

        private String ReplaceFormatString(String formatString) {
            formatString = formatString.Replace("%p", "{0}");	//Priority
            formatString = formatString.Replace("%m", "{1}");	//Message
            formatString = formatString.Replace("%t", "{2}");	//Time
            formatString = formatString.Replace("%c", "{3}");	//Code File
            formatString = formatString.Replace("%l", "{4}");	//Line No.
            return formatString;
        }
    }
}
